//
//  GXNotOrderViewCell.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/11/26.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXNotOrderViewCell.h"
#import "OrderAndUploadPhotoMananger.h"
@interface GXNotOrderViewCell ()
@property (weak, nonatomic) IBOutlet UILabel *xdLabel;
@property (weak, nonatomic) IBOutlet UILabel *stateLabel;
@property (weak, nonatomic) IBOutlet UIImageView *logoImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *sexImageView;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *tableNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *peopelNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *menuLabel;
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalLabel;
@property (weak, nonatomic) IBOutlet UIButton *orderBtn;
@property (weak, nonatomic) IBOutlet UIButton *refuseBtn;

@end
@implementation GXNotOrderViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setModel:(OrderMessageModel *)model
{
    _model = model;
    self.xdLabel.text = model.xdDate;
    self.stateLabel.text = [self fromOrderStateGetString:model.state];
    NSString *url = [NSString stringWithFormat:@"%@/%@",kBaseURLIMAGE,model.logo];
    [self.logoImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeHolder_userLogo"]];
    self.nameLabel.text = model.name;
    self.phoneLabel.text = model.phone;
    NSString *imagestr = [model.sex isEqualToString:@"0"] ? @"ord_sexboy":@"ord_sexgirl";
    self.sexImageView.image = [UIImage imageNamed:imagestr];
    NSString *timeStr = [model.periodTime isEqualToString:@"1"] ? @"晚上":@"中午";
    self.timeLabel.text = [NSString stringWithFormat:@"%@ %@",model.orDate,timeStr];
    self.tableNumberLabel.text = [NSString stringWithFormat:@"%@号桌",model.tabNum];
    self.peopelNumLabel.text = [NSString stringWithFormat:@"%@人",model.peopleNum];
    self.totalLabel.text = [NSString stringWithFormat:@"¥%@",model.total];
    [self handleMenuDataSourceFormDishes:model.dishes];
}
- (NSString *)fromOrderStateGetString:(NSString *)str
{
    if ([str isEqualToString:@"0"]) {
        return @"待接单";
    }else if ([str isEqualToString:@"1"]){
        return @"已接单";
    }else if ([str isEqualToString:@"3"]){
        return @"已拒单";
    }
    return nil;
}
- (void)handleMenuDataSourceFormDishes:(NSString *)disherStr {
    NSArray *array = [disherStr  componentsSeparatedByString:@","];
    NSMutableArray *nameArr = [NSMutableArray array];
    NSMutableArray *priceArr = [NSMutableArray array];
    for (int i=0; i<array.count; i++) {
        NSString *tempStr = array[i];
        NSArray *nameArray = [tempStr componentsSeparatedByString:@"-"];
        [nameArr addObject:nameArray[1]];
        [priceArr addObject:nameArray[2]];
        
    }
    self.menuLabel.text = [nameArr componentsJoinedByString:@" "];
    NSNumber *sum = [priceArr valueForKeyPath:@"@sum.floatValue"];
    self.numberLabel.text = [NSString stringWithFormat:@"x%@",sum];
}

- (IBAction)pressOrderBtn:(UIButton *)sender {
    [self changeOrdState:@"1"];
}
- (IBAction)pressRefuseBtn:(UIButton *)sender {
    [self changeOrdState:@"3"];
}
- (void)changeOrdState:(NSString *)state
{
    NSString *mess;
    if ([state isEqualToString:@"3"]) {
        mess = @"确认要拒绝该订单吗？";
    }
    else if([state isEqualToString:@"1"]){
        mess = @"确认接受该订单吗？";
    }
    
    [UIAlertController showAlertInViewController:self.getViewController withTitle:nil message:mess cancelButtonTitle:@"取消" destructiveButtonTitle:@"确认" otherButtonTitles:nil tapBlock:^(UIAlertController * _Nonnull controller, UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
        if (buttonIndex==1) {
            [OrderAndUploadPhotoMananger ChangeShopOrderState:state oNumber:self.model.oNumber success:^(id responseObject) {
                self.model.state = state;
                if ([state isEqualToString:@"3"]) {
                    self.orderBtn.hidden = YES;
                    self.stateLabel.text = @"已拒单";
                }else
                {
                    self.refuseBtn.hidden = YES;
                    self.stateLabel.text = @"已接单";
                }
            } fail:^(id responseObject) {
            }];
        }
    }];
}
@end
